#!/usr/bin/env python
# coding=utf-8

import re
import os
import logging
import sys
import codecs

def init_log():
    logger = logging.getLogger()
    hdlr = logging.FileHandler('link_check.log')
    formatter = logging.Formatter('%(asctime)s %(levelname)s:: %(message)s')
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.INFO)
    return logger

logger = init_log()

def checklink(filename):
    urlBase = r"http://172.26.10.31/LightAplSrc/trunk/"
    url = urlBase + filename
    logger.info('svn file : %s', url)
    hasVoiceLib = False
    hasVoiceLibBase = False
    lines = os.popen(r'svn cat ' + url).readlines()
    for lnum in range(len(lines)):
        line = lines[lnum]
        if re.search(r'voicelib\.lib', line, re.I):
#        if line.find(r'VoiceLib.lib') >=0 or line.find(r'VoiceLib.Lib') >= 0:
            hasVoiceLib = True
            logger.info(r'Find VoiceLib.lib in %d %s', lnum, line)
        elif re.search(r'voicelibbase\.lib', line, re.I):
#        elif line.find(r'VoiceLibBase.lib')>=0 or line.find(r'VoiceLibBase.Lib')>=0:
            hasVoiceLibBase = True
            logger.info(r'Find VoiceLibBase.lib in %d %s', lnum, line)
    ret =  not (hasVoiceLib and hasVoiceLibBase)
    if not ret:
        logger.warning('Find conflict !!!!')
        print 'Error', url
    return ret


def main():
    logger.info('hello')
    for fn in open('filepaths.txt').readlines():
        checklink(fn)
    print 'Check complete.'
        
        

if __name__ == '__main__':
    main()
    
